{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Cheat Sheet\n",
    "\n",
    "Basic cheatsheet for Python mostly based on the book written by Al Sweigart, [Automate the Boring Stuff with Python](https://automatetheboringstuff.com/) under the [Creative Commons license](https://creativecommons.org/licenses/by-nc-sa/3.0/) and many other sources.\n",
    "\n",
    "## Read It\n",
    "\n",
    "- [Website](https://www.pythoncheatsheet.org)\n",
    "- [Github](https://github.com/wilfredinni/python-cheatsheet)\n",
    "- [PDF](https://github.com/wilfredinni/Python-cheatsheet/raw/master/python_cheat_sheet.pdf)\n",
    "- [Jupyter Notebook](https://mybinder.org/v2/gh/wilfredinni/python-cheatsheet/master?filepath=jupyter_notebooks)\n",
    "\n",
    "## Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello(name):\n",
    "    print('Hello {}'.format(name))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Return Values and return Statements\n",
    "\n",
    "When creating a function using the def statement, you can specify what the return value should be with a return statement. A return statement consists of the following:\n",
    "\n",
    "- The return keyword.\n",
    "\n",
    "- The value or expression that the function should return."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "def getAnswer(answerNumber):\n",
    "    if answerNumber == 1:\n",
    "        return 'It is certain'\n",
    "    elif answerNumber == 2:\n",
    "        return 'It is decidedly so'\n",
    "    elif answerNumber == 3:\n",
    "        return 'Yes'\n",
    "    elif answerNumber == 4:\n",
    "        return 'Reply hazy try again'\n",
    "    elif answerNumber == 5:\n",
    "        return 'Ask again later'\n",
    "    elif answerNumber == 6:\n",
    "        return 'Concentrate and ask again'\n",
    "    elif answerNumber == 7:\n",
    "        return 'My reply is no'\n",
    "    elif answerNumber == 8:\n",
    "        return 'Outlook not so good'\n",
    "    elif answerNumber == 9:\n",
    "        return 'Very doubtful'\n",
    "\n",
    "r = random.randint(1, 9)\n",
    "fortune = getAnswer(r)\n",
    "print(fortune)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The None Value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "spam = print('Hello!')\n",
    "spam is None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note: never compare to `None` with the `==` operator. Always use `is`.\n",
    "\n",
    "### print Keyword Arguments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Hello', end='')\n",
    "print('World')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('cats', 'dogs', 'mice')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('cats', 'dogs', 'mice', sep=',')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Local and Global Scope\n",
    "\n",
    "- Code in the global scope cannot use any local variables.\n",
    "\n",
    "- However, a local scope can access global variables.\n",
    "\n",
    "- Code in a function’s local scope cannot use variables in any other local scope.\n",
    "\n",
    "- You can use the same name for different variables if they are in different scopes. That is, there can be a local variable named spam and a global variable also named spam.\n",
    "\n",
    "### The global Statement\n",
    "\n",
    "If you need to modify a global variable from within a function, use the global statement:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def spam():\n",
    "    global eggs\n",
    "    eggs = 'spam'\n",
    "\n",
    "eggs = 'global'\n",
    "spam()\n",
    "print(eggs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are four rules to tell whether a variable is in a local scope or global scope:\n",
    "\n",
    "1. If a variable is being used in the global scope (that is, outside of all functions), then it is always a global variable.\n",
    "\n",
    "1. If there is a global statement for that variable in a function, it is a global variable.\n",
    "\n",
    "1. Otherwise, if the variable is used in an assignment statement in the function, it is a local variable.\n",
    "\n",
    "1. But if the variable is not used in an assignment statement, it is a global variable."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
